package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderDto;
import com.itheima.pojo.User;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/submit.do")
    public Result submit(@RequestBody OrderDto orderDto){
        if (orderDto==null){
            return new Result(false,"参数异常" );
        }
        //校验手机号与验证码是否匹配
        String telephone = orderDto.getTelephone();
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute(telephone);
        //如果不匹配 直接返回错误信息
        if (!code.equals(orderDto.getValidateCode())){
            return new Result(false,"验证码错误" );
        }

        Integer memberId = (Integer) session.getAttribute("memberId");
        orderDto.setMemberId(memberId);  //设置memberId到order表
        return orderService.submit(orderDto);
    }

    @PostMapping("/findById.do")
    public Result findById(Integer id){
        return orderService.findById(id);
    }
}
